<?php

namespace app\base\controller;

use app\common\controller\AdminBase;
use app\base\model as baseModel;
use app\common\model\Base;
use think\Cache;
use think\Session;

class Login extends AdminBase {
  private $userModel, $modelRoleUser;

  public function __construct() {
    parent::__construct();
    $this->userModel = new baseModel\User();
    $this->modelRoleUser = new baseModel\RoleUser();
  }

  public function _initialize() {
    parent::_initialize(); // TODO: Change the autogenerated stub
  }

  public function index() {
    $refer = input('refer', NULL);

    if (session('?userid')) {
      $roldId = session('user_info')['role_id'];
      $href = ($roldId == 3) ? 'portal/knowledge/index' : 'admin/room/index';
      $this->success($refer ? urldecode($refer) : $href);
    }

    if (!request()->isPost()) {
      return view();
    } else {
      $this->loginCheck(FALSE);
      $roldId = session('user_info')['role_id'];
      $href = ($roldId == 3) ? 'portal/knowledge/index' : 'admin/room/index';
      $refer = $refer ? urldecode($refer) : $href;
      if (request()->isAjax()) {
        $returnData = ['userinfo' => session('user_info')];
        $this->success(lang('success'), $refer, $returnData);
      }
      $this->success(urldecode($refer));
    }
  }

  public function logout() {
    session('user_id', NULL);
    session('user_info', NULL);
    $this->success(lang('success'), 'base/login/index', '', 0);
  }

  private function loginCheck($captcha = FALSE) {
    $form = input('form/a', NULL, 'trim');
    if (!$form) $this->error(lang('form empty'));
    if ($captcha && !captcha_check($form['captcha'])) {
      $this->error(lang('captcha error'));
    }
    //校验
    $validate = $this->validate($form, 'Login');
    if (TRUE !== $validate) {
      $this->error($validate);
    }
    //错误登录记录
    $login_cache_name = $form['username'] . '_login_error_times';
    $login_error_times = Cache::get($login_cache_name);
    if (!$login_error_times) $login_error_times = 0;
    //
    $userinfo = $this->userModel->getUserInfoByName($form['username'], 'id,username,realname,lock,role_id,status');
    if (!$userinfo) {
      Cache::set($login_cache_name, $login_error_times + 1, 86400);
      $this->error(lang('user or password error'));
    }
    //
    if (FALSE == $this->userModel->checkPassword($userinfo['id'], $form['password'])) {
      if ($login_error_times >= 5) {
        $this->userModel->lockUser($userinfo['id']);
        Cache::set($login_cache_name, NULL);
      } else {
        Cache::set($login_cache_name, $login_error_times + 1, 86400);
      }
      $this->error(lang('user or password error'));
    }
    //是否锁定
    if ($userinfo['lock'] > 0) {
      //$this->error(lang('user locked'));
    }
    //登陆成功
    //$userRole = $this->modelRoleUser->getUserRole($userinfo['id']);
    session('user_id', $userinfo['id']);
    session('user_info', $userinfo);
    //
    cmf_log('登录', $userinfo['id'], '', $userinfo['id']);

    return $userinfo;
  }


}